Ext.namespace("Ext.sw");

Ext.sw.upFile = function(config){
	config = config || {}
	if(config.url){
		this.formUrl = config.url;
	}
	if(config.urlCallback){
		this.doUpFileSucc = config.urlCallback;
	}
	if(config.upName){
		this.upName = name;
	}
	if(config.upId){
		this.upId = upId;
	}
	Ext.sw.upFile.superclass.constructor.apply(this,arguments);
}

Ext.extend(Ext.sw.upFile,Ext.Window,{
	formUrl : "",
	upName:"up",
	upId:"up",
	title:"图片上传",
	doUpFileSucc : function(val){
		alert(val);
	},
	initComponent : function(){
	    Ext.sw.upFile.superclass.initComponent.call(this);
		
		this.upFileFrm = new Ext.form.FormPanel({
			baseCls: 'x-plain',
			labelWidth: 80,
			url:this.formUrl,
			fileUpload:true,
			defaultType: 'textfield',
			method:'POST',
			layout:'form',
			items: [{
				xtype: 'textfield',
				fieldLabel: '文件名',
				name:this.upName,
				id:this.upId,
				inputType: 'file',
				allowBlank: false,
				blankText: '文件不能为空!',
				anchor: '90%'
			}],
			buttons: [{
				text: '上传',
				handler: function(){
					var upFileForm = this.ownerCt.ownerCt;
					var win = upFileForm.ownerCt;
					if(upFileForm.getForm().isValid()){
						
						upFileForm.getForm().submit({
							waitTitle : '请等待' ,
			   				 waitMsg: '正在加载中',
							success: function(form, action){
							   Ext.Msg.alert('操作成功','上传文件成功');
							   win.hide();
							   var jsons = Ext.util.JSON.decode(action.response.responseText)
							   win.doUpFileSucc(jsons.filePath);
							},    
						   failure: function(form,action){
						   	  var jsons = Ext.util.JSON.decode(action.response.responseText)
							  Ext.Msg.alert('操作失败', jsons.msg);    
						   }
						})		       
					}
			   
				}
			},{
				text: '关闭',
				handler:function(){
					this.ownerCt.ownerCt.ownerCt.hide();
				}
			}]
		});
		
	    this.add(this.upFileFrm);
	}
})

